import ArrayUtil from '@/ArrayUtil';
import BaseUtil from '@/BaseUtil';
import Buff from '@/Buff';
import BuffUtil from '@/BuffUtil';
import MathUtil from '@/MathUtil';
import SelectUtil from '@/SelectUtil';
import MovementCommon from 'code/Core/Common/MovementCommon';
import AbilityBase from 'code/Scripts/Abilities/base/AbilityBase';
/**
 * 冰咆哮
 **/
export default class BossSpell_IceRora extends AbilityBase {
    static instance: BossSpell_IceRora;
    buffType: AppBuffType = {
        id: 'buff_BossSpell_IceRora',
        art: this.art,
        name: this.abilityName,
        ubertip: this.ubertip,
        targetArt: 'Abilities\\Spells\\Other\\BreathOfFrost\\BreathOfFrostTarget.mdl',
        targetAttach: 'origin',
        interval: 1,
        dur: 5,
        onInterval: this.buffInterval.bind(this),
        attribute: {
            move_speed_p: -0.8,
            attackSpd_p: -2,
        },
        isDebuff: true,
    };
    buffInterval(buff: Buff) {
        BuffUtil.registerBuffType(this.buffType);
    }
    constructor(id: string = 'Myh6') {
        super(id);
        BossSpell_IceRora.instance = this;
        this.init();
        se.onUnitSpellEffect(this.onSpellEffect.bind(this), this.id);
    }
    init() {
        BuffUtil.registerBuffType(this.buffType);
    }
    cast<T extends Object = any>(tarX: number, tarY: number, source: unit, target: unit, data?: T): void {
        BossAbilityManagerInstance.text('冰咆哮', source);
        DestroyEffect(AddSpecialEffectTarget('WhiteChakraExplosion.mdx', source, 'overhead'));
        let es = SelectUtil.getEnemyUnitsInRange(source, 450);
        for (let e of es) {
            BuffUtil.addBuff(e, this.buffType.id, source);
            let ang = MathUtil.angleBetweenCoords(GetUnitX(source), GetUnitY(source), GetUnitX(e), GetUnitY(e));
            let dis = 800 - MathUtil.distanceBetweenUnits(source, e);
            MovementCommon.hitFly({
                target: e,
                distance: dis,
                caster: source,
                maxHeight: 300,
                speed: 800,
                angle: ang,
            });
        }

        BaseUtil.runLater(1, () => {
            this.effect(source);
            source = null;
        });
    }
    effect(source: unit) {
        BaseUtil.onTimer(0.2, (c) => {
            let es = SelectUtil.getEnemyUnitsInRange(source, 1200);
            if (es.length == 0) return true;
            let tar = ArrayUtil.randomElement(es);
            let x = GetUnitX(tar);
            let y = GetUnitY(tar);
            MovementCommon.定点限高投射物({
                missile: 'Abilities\\Weapons\\FrostWyrmMissile\\FrostWyrmMissile.mdl',
                maxHeight: 600,
                caster: source,
                speed: 400,
                tarX: x,
                tarY: y,
                onReachTarget: this.onMissileReachTarget.bind(this),
            });
            return c < 32;
        });
    }
    onMissileReachTarget(p: 定点限高投射物参数) {
        DestroyEffect(AddSpecialEffect('Abilities\\Spells\\Undead\\FrostNova\\FrostNovaTarget.mdl', p.curX, p.curY));
        let es = SelectUtil.getEnemyUnitsInRange(p.caster, 125, p.curY, p.curY);
        for (let e of es) {
            BuffUtil.addBuff(e, this.buffType.id, p.caster);
            DamageSystemInstance.applyIceDamage(e, p.caster, 1000);
        }
    }
    onSpellEffect() {
        this.cast(GetSpellTargetX(), GetSpellTargetY(), GetTriggerUnit(), GetSpellTargetUnit());
    }
}
